/**    
 * @{#} RoleDaoImpl4Mybatis.java Create on 2012-6-8 上午12:17:53    
 *    
 * Copyright (c) 2012 by 悠忧虫.    
 *    
 * @author 悠忧虫
 * @version 1.0    
 */
package tk.sunlatus.project.permission.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import tk.sunlatus.project.common.IDTools;
import tk.sunlatus.project.common.mappers.RoleMapper;
import tk.sunlatus.project.permission.dao.RoleDao;
import tk.sunlatus.project.permission.domain.Role;
import tk.sunlatus.project.permission.domain.RoleUserRef;

/**
 * @{# RoleDaoImpl4Mybatis.java Create on 2012-6-8 上午12:17:53
 * 
 *     类功能说明:
 * 
 *     <p>
 *     Copyright: Copyright(c) 2012
 *     </p>
 * @Version 1.0
 * @Author 悠忧虫
 */
public class RoleDaoMybatisImpl implements RoleDao {
    private SqlSessionFactory sqlSessionFactory = null;

    @Override
    public Role saveRole(Role role) {

        SqlSession sqlSession = getSqlSessionFactory().openSession();

        RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);

        int result = roleMapper.saveRole(role);
        sqlSession.close();

        return result == 0 ? null : role;
    }

    @Override
    public Role updateRole(Role role) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int removeRoles(Role role) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Role removeRoleByID(String roleID) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Role queryByID(String roleID) {
        SqlSession sqlSession = getSqlSessionFactory().openSession();

        RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);

        Role role = roleMapper.queryByID(roleID);

        sqlSession.close();
        return role;
    }

    @Override
    public List<Role> queryRoles(Role role) {

        SqlSession sqlSession = getSqlSessionFactory().openSession();

        RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);

        List<Role> roleList = roleMapper.queryRoles(role);
        sqlSession.close();
        return roleList;

    }

    @Override
    public void addUsers2Role(Role role, String[] roleUserIDs) {

        SqlSession sqlSession = getSqlSessionFactory().openSession();

        RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
        List<RoleUserRef> params = new ArrayList<RoleUserRef>(roleUserIDs.length);
        RoleUserRef ref = null;
        for (String userID : roleUserIDs) {
            ref = new RoleUserRef();
            ref.setId(IDTools.getUUID());
            ref.setRoleID(role.getId());
            ref.setUserID(userID);
            params.add(ref);
        }
        roleMapper.addUsers2Role(params);
        sqlSession.close();
    }

    /**
     * @return the sqlSessionFactory
     */
    public SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }

    /**
     * @param sqlSessionFactory
     *            the sqlSessionFactory to set
     */
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }
}
